<template>
    <!-- 对话框表单 -->
    <el-dialog class="ba-operate-dialog" top="10vh" :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)" @close="baTable.toggleForm"
        :destroy-on-close="true">
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div class="ba-operate-form" :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'">
                <el-form ref="formRef" @submit.prevent="" @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items" :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'" :rules="rules">
                    <FormItem type="remoteSelect" prop="user_id" label="发送用户" v-model="baTable.form.items!.user_id"
                        :placeholder="t('Click select')" :input-attr="{
                            pk: 'id',
                            field: 'nickname',
                            // 搜索字段
                            searchField: 'nickname',
                            remoteUrl: 'user/user/index',
                        }" />
                    <FormItem type="remoteSelect" prop="user_id" label="接收用户" v-model="baTable.form.items!.to_user_id"
                        :placeholder="t('Click select')" :input-attr="{
                            pk: 'id',
                            field: 'nickname',
                            // 搜索字段
                            searchField: 'nickname',
                            remoteUrl: 'user/user/index',
                        }" />
                    <el-form-item prop="content" label="消息内容">
                        <el-input v-model="baTable.form.items!.content" type="textarea" :autosize="{ minRows: 2, maxRows: 4 }" />
                    </el-form-item>
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)"
                    type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') :
                    t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, inject, useTemplateRef } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import type { FormItemRule } from 'element-plus'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'

const config = useConfig()
const formRef = useTemplateRef('formRef')
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
})

</script>

<style scoped lang="scss">
:deep(.penultimate-node) {
    .el-tree-node__children {
        padding-left: 60px;
        white-space: pre-wrap;
        line-height: 12px;

        .el-tree-node {
            display: inline-block;
        }

        .el-tree-node__content {
            padding-left: 5px !important;
            padding-right: 5px;

            .el-tree-node__expand-icon {
                display: none;
            }
        }
    }
}
</style>
